﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Expression;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;
using Common;
using Mang24k99.Model.GOLD;

namespace Mang24k99.Model.COMM
{
    [ActiveRecord(Table = "KT_COMM_ORGLIST")]
    public partial class MKtCommOrglist : ActiveRecordBase
    {

        private int _OrgnizeNO;
        private IList<MKtGoldProduct> _MKtGoldProduct;
        private int _ID;

        private string _OrgnizeName;

        private string _OrgnizeAlias;

        private int? _ParentNO;

        private string _ChineseSpell;

        private string _CompsiteNO;

        private int? _LevelOrder;

        private int? _OrangeLayer;

        private Byte[] _LOGO;

        private string _LogoPath;

        private int? _IsValidate;

        private DateTime? _InputTime;

        private DateTime? _ModifyTime;

        private DateTime? _DeleteTime;

        private int? _IsGrant;

        private int? _IsColumn;

        /// <summary>
        /// Gets or sets the OrgnizeNO.()
        /// </summary>
        /// <value>The OrgnizeNO.</value>
        [PrimaryKey(PrimaryKeyType.Assigned, "ORGNIZE_NO")]
        public int OrgnizeNO
        {
            get
            {
                return _OrgnizeNO;
            }
            set
            {
                _OrgnizeNO = value;
            }
        }

        [HasMany(typeof(MKtGoldProduct), ColumnKey = "ORGNIZE_NO", Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Inverse = true, Lazy = true)]
        public IList<MKtGoldProduct> MKtGoldProduct
        {
            get
            {
                return _MKtGoldProduct;
            }
            set
            {
                _MKtGoldProduct = value;
            }
        }
        /// <summary>
        /// Gets or sets the ID.()
        /// </summary>
        /// <value>The ID.</value>
        [Property("ID", Insert = false, Update = false)]
        public int ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }


        /// <summary>
        /// Gets or sets the OrgnizeName.()
        /// </summary>
        /// <value>The OrgnizeName.</value>
        [Property("ORGNIZE_NAME")]
        public string OrgnizeName
        {
            get
            {
                return _OrgnizeName;
            }
            set
            {
                _OrgnizeName = value;
            }
        }


        /// <summary>
        /// Gets or sets the OrgnizeAlias.()
        /// </summary>
        /// <value>The OrgnizeAlias.</value>
        [Property("ORGNIZE_ALIAS")]
        public string OrgnizeAlias
        {
            get
            {
                return _OrgnizeAlias;
            }
            set
            {
                _OrgnizeAlias = value;
            }
        }


        /// <summary>
        /// Gets or sets the ParentNO.()
        /// </summary>
        /// <value>The ParentNO.</value>
        [Property("PARENT_NO")]
        public int? ParentNO
        {
            get
            {
                return _ParentNO;
            }
            set
            {
                _ParentNO = value;
            }
        }


        /// <summary>
        /// Gets or sets the ChineseSpell.()
        /// </summary>
        /// <value>The ChineseSpell.</value>
        [Property("CHINESE_SPELL")]
        public string ChineseSpell
        {
            get
            {
                return _ChineseSpell;
            }
            set
            {
                _ChineseSpell = value;
            }
        }


        /// <summary>
        /// Gets or sets the CompsiteNO.()
        /// </summary>
        /// <value>The CompsiteNO.</value>
        [Property("COMPSITE_NO")]
        public string CompsiteNO
        {
            get
            {
                return _CompsiteNO;
            }
            set
            {
                _CompsiteNO = value;
            }
        }


        /// <summary>
        /// Gets or sets the LevelOrder.()
        /// </summary>
        /// <value>The LevelOrder.</value>
        [Property("LEVEL_ORDER")]
        public int? LevelOrder
        {
            get
            {
                return _LevelOrder;
            }
            set
            {
                _LevelOrder = value;
            }
        }


        /// <summary>
        /// Gets or sets the OrangeLayer.()
        /// </summary>
        /// <value>The OrangeLayer.</value>
        [Property("ORANGE_LAYER")]
        public int? OrangeLayer
        {
            get
            {
                return _OrangeLayer;
            }
            set
            {
                _OrangeLayer = value;
            }
        }


        /// <summary>
        /// Gets or sets the LOGO.()
        /// </summary>
        /// <value>The LOGO.</value>
        [Property("LOGO")]
        public Byte[] LOGO
        {
            get
            {
                return _LOGO;
            }
            set
            {
                _LOGO = value;
            }
        }


        /// <summary>
        /// Gets or sets the LogoPath.()
        /// </summary>
        /// <value>The LogoPath.</value>
        [Property("LOGO_PATH")]
        public string LogoPath
        {
            get
            {
                return _LogoPath;
            }
            set
            {
                _LogoPath = value;
            }
        }


        /// <summary>
        /// Gets or sets the IsValidate.()
        /// </summary>
        /// <value>The IsValidate.</value>
        [Property("IS_VALIDATE")]
        public int? IsValidate
        {
            get
            {
                return _IsValidate;
            }
            set
            {
                _IsValidate = value;
            }
        }


        /// <summary>
        /// Gets or sets the InputTime.()
        /// </summary>
        /// <value>The InputTime.</value>
        [Property("INPUT_TIME")]
        public DateTime? InputTime
        {
            get
            {
                return _InputTime;
            }
            set
            {
                _InputTime = value;
            }
        }


        /// <summary>
        /// Gets or sets the ModifyTime.()
        /// </summary>
        /// <value>The ModifyTime.</value>
        [Property("MODIFY_TIME")]
        public DateTime? ModifyTime
        {
            get
            {
                return _ModifyTime;
            }
            set
            {
                _ModifyTime = value;
            }
        }


        /// <summary>
        /// Gets or sets the DeleteTime.()
        /// </summary>
        /// <value>The DeleteTime.</value>
        [Property("DELETE_TIME")]
        public DateTime? DeleteTime
        {
            get
            {
                return _DeleteTime;
            }
            set
            {
                _DeleteTime = value;
            }
        }
        /// <summary>
        /// Gets or sets the IsGrant.(1：是授权机构，在权限管理出现（默认）
        //0：是非授权机构，在权限管理中不出现
        //)
        /// </summary>
        /// <value>The IsGrant.</value>
        [Property("IS_GRANT")]
        public int? IsGrant
        {
            get
            {
                return _IsGrant;
            }
            set
            {
                _IsGrant = value;
            }
        }
        
        
        /// <summary>
        /// Gets or sets the IsColumn.(1：是栏目机构，在栏目管理中出现（默认）
        ///0：是非栏目机构，在栏目管理中不出现
        ///)
        /// </summary>
        /// <value>The IsColumn.</value>
        [Property("IS_COLUMN")]
        public int? IsColumn
        {
            get
            {
                return _IsColumn;
            }
            set
            {
                _IsColumn = value;
            }
        }

        public static IList<MKtCommOrglist> SelectAll()
        {
            return (IList<MKtCommOrglist>)FindAll(typeof(MKtCommOrglist));
        }
        public static MKtCommOrglist SelectByPrimaryKey(object ID)
        {
            return (MKtCommOrglist)FindByPrimaryKey(typeof(MKtCommOrglist), ID);
        }
        public static IList<MKtCommOrglist> SelectALLByProperty(string property, object value)
        {
            return (IList<MKtCommOrglist>)FindAllByProperty(typeof(MKtCommOrglist), property, value);
        }
        public static IList<MKtCommOrglist> SelectALLSliced(int firstResult, int maxResult, NHibernate.Expression.DetachedCriteria criteria)
        {

            return (IList<MKtCommOrglist>)SlicedFindAll(typeof(MKtCommOrglist), firstResult, maxResult, criteria);
        }
        //protected override void OnDelete()
        //{
        //    MKtCommOrglistHistory mHistory = new MKtCommOrglistHistory();
        //    Common.ModelCommon.CopyObjectValues(this, mHistory);
        //    mHistory.Create();
        //    base.OnDelete();
        //}

        public static void Create(MKtCommOrglist orglist)
        {
            orglist.Create();
        }
        /// <summary>
        /// 选择最大的机构编号
        /// </summary>
        /// <returns></returns>
        public static int GetMaxNum()
        {
            string Hql = @"select max(orglist.OrgnizeNO) from MKtCommOrglist orglist  ";
            string count = @"select count(orglist.OrgnizeNO) from MKtCommOrglist orglist ";
            ScalarQuery querycount = new ScalarQuery(typeof(MKtCommOrglist), count);
            if (Convert.ToInt32(ExecuteQuery(querycount)) == 0)
            {
                return 0;
            }
            ScalarQuery query = new ScalarQuery(typeof(MKtCommOrglist),Hql);
            return (int)ExecuteQuery(query);
        }
        /// <summary>
        /// 返回最大的排序号
        /// </summary>
        /// <param name="hql"></param>
        /// <returns></returns>
        public static int SelectMaxOrder(string where)
        {
            string hql=@"select max(orglist.LevelOrder) from MKtCommOrglist orglist where "+where;
            string count = @"select count(*) from MKtCommOrglist orglist where " + where;
            ScalarQuery querycount = new ScalarQuery(typeof(MKtCommOrglist), count);
            if (Convert.ToInt32(ExecuteQuery(querycount)) == 0)
            {
                return 0;
            }
            ScalarQuery query = new ScalarQuery(typeof(MKtCommOrglist),hql);
            return Convert.ToInt32(ExecuteQuery(query));
        }
        /// <summary>
        /// 修改一条记录
        /// </summary>
        /// <param name="orglist"></param>
        public  static void Update(MKtCommOrglist orglist ) 
        {
            orglist.Update();
        }
        //删除一条记录
        public static void Delete(MKtCommOrglist orglist)
        {
            MKtCommOrglistHistory history = new MKtCommOrglistHistory();
            Common.ModelCommon.CopyObjectValues(orglist,history);
            history.DeleteTime = DateTime.Now;
            history.Create();
            orglist.Delete();
        }

        public static IList<MKtCommOrglist> SelectByWhere(string where)
        {
            string hql = @"from MKtCommOrglist orglist where "+where;
            SimpleQuery query = new SimpleQuery(typeof(MKtCommOrglist), hql);
            return (IList<MKtCommOrglist>)ExecuteQuery(query);
        }
    }
}
